package org.chat.hhdchat.Controller;
import org.chat.hhdchat.Service.MessageService;
import org.chat.hhdchat.component.R;
import org.chat.hhdchat.entity.ChatMessage;
import org.chat.hhdchat.entity.HistoricalMessageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/messages")
public class MessageController {
    private final MessageService messageService;

    @Autowired
    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    /**
     * 获取两个用户之间的历史消息
     * GET /api/messages?from=user1&to=user2
     */
    @PostMapping("/historical")
    public R<List<ChatMessage>> getChatHistory(@RequestBody HistoricalMessageRequest historicalMessageRequest) {
        List<ChatMessage> messages = messageService.getMessagesBetweenUsers(historicalMessageRequest.getFrom(), historicalMessageRequest.getTo());
        return R.success(messages);
    }

    /**
     * 删除一条消息
     * DELETE /api/messages/{messageId}
     */
    @DeleteMapping("/{messageId}")
    public R<Void> deleteMessage(@PathVariable String messageId) {
        messageService.deleteMessage(messageId);
        return R.error(204,"No Content");  // 返回 204 No Content
    }

    @GetMapping("/test")
    public R<String> testTest(){
        return R.success("pong");
    }
}
